package com.youlai.boot.system.service.order;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import com.youlai.boot.common.exception.BusinessException;
import com.youlai.boot.system.model.entity.LpsWl;
import com.youlai.boot.system.model.vo.CommonOrderVO;
import com.youlai.boot.system.model.vo.LpsOrderXcpVO;
import com.youlai.boot.util.MapUtils;
import com.youlai.boot.util.NumberUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class XcpOrderHandle {
    public final static String BK = "BK";
    public final static String TK = "TK";
    public final static String GQ = "GQ";
    public final static String P00_TSD = "P00_TSD";
    public final static String CODE = "CODE";
    public final static String P00_RETAINER_TYPE = "P00_RETAINER_TYPE";
    public final static String P00_INTERCOM = "P00_INTERCOM";
    public final static String P00_APRON_TYPE = "P00_APRON_TYPE";
    public final static String P00_TB_TYPE = "P00_TB_TYPE";
    public final static String P00_STRUCTURE_TYPE = "P00_STRUCTURE_TYPE";
    public final static String DRIVE_POS = "DRIVE_POS";

    public final static String WL_57860119 = "57860119";
    public final static String WL_57860120 = "57860120";
    public final static String WL_57860121 = "57860121";
    public final static String WL_57860122 = "57860122";
    public final static String WL_57860170 = "57860170";
    public final static String WL_57860171 = "57860171";
    public final static String WL_57860919 = "57860919";
    public final static String WL_57860920 = "57860920";
    public final static String WL_57860921 = "57860921";
    public final static String WL_57860922 = "57860922";
    public final static String WL_57860929 = "57860929";
    public final static String WL_C57640701_001 = "C57640701-001";
    public final static String WL_C57640701_002 = "C57640701-002";

    /**
     * @param objList
     * @param lpsWlMap
     * @return
     */
    public List<LpsOrderXcpVO> handle(List<CommonOrderVO> objList, Map<String, LpsWl> lpsWlMap) {
        if (CollectionUtil.isEmpty(objList)) {
            return CollectionUtil.newArrayList();
        }
        List<LpsOrderXcpVO> list = objList.stream().map(item -> {
            LpsOrderXcpVO order = new LpsOrderXcpVO();
            BeanUtils.copyProperties(item, order);
            if (Objects.nonNull(lpsWlMap.get(order.getWl()))) {
                order.setBz(lpsWlMap.get(order.getWl()).getRemark());
            }
            Map<String, String> featureMap = MapUtils.getFeatureMap(item.getTz());

            switch (order.getWl()) {
                case WL_57860119, WL_57860120, WL_57860170, WL_57860171:
                    WL_57860119(featureMap, order);
                    break;
                case WL_57860121, WL_57860122:
                    break;
                case WL_57860919, WL_57860920:
                    WL_57860919(featureMap, order);
                    break;
                case WL_57860921, WL_57860922:
                    WL_57860921(featureMap, order);
                    break;
                case WL_57860929:
                    WL_57860929(featureMap, order);
                    break;
                case WL_C57640701_001, WL_C57640701_002:
                    break;
                default:
                    log.error("【新产品】解析到未知的物料{}", order.getWl());
            }
            //order.setId(IdUtil.getSnowflakeNextId());
            return order;
        }).toList();
        // 拆分为两个列表
        Comparator<LpsOrderXcpVO> comparator = Comparator
                .comparing(LpsOrderXcpVO::getXsdd, Comparator.nullsLast(String::compareTo))
                .thenComparing(LpsOrderXcpVO::getSoxxm, Comparator.nullsLast(String::compareTo));
        List<LpsOrderXcpVO> targetList = list.stream()
                .filter(order -> WL_57860929.equals(order.getWl()))
                .sorted(comparator)
                .toList();
        List<LpsOrderXcpVO> otherList = list.stream()
                .filter(order -> !WL_57860929.equals(order.getWl()))
                .toList();

        // 合并列表
        List<LpsOrderXcpVO> sortedList = new ArrayList<>(targetList);
        sortedList.addAll(otherList);
        if (CollectionUtil.isNotEmpty(sortedList)) {
            sortedList.forEach(order -> {
                order.setId(IdUtil.getSnowflakeNextId());
            });
        }
        return sortedList;
    }

    private void WL_57860929(Map<String, String> featureMap, LpsOrderXcpVO order) {
        try {
            order.setBk(featureMap.getOrDefault(BK, ""));
            order.setTk(featureMap.getOrDefault(TK, ""));
            order.setGq(featureMap.getOrDefault(GQ, ""));

            order.setP00Tsd(featureMap.getOrDefault(P00_TSD, ""));
            order.setCode(featureMap.getOrDefault(CODE, ""));
            order.setP00RetainerType(featureMap.getOrDefault(P00_RETAINER_TYPE, ""));
            order.setP00Intercom(featureMap.getOrDefault(P00_INTERCOM, ""));
            order.setP00ApronType(featureMap.getOrDefault(P00_APRON_TYPE, ""));

            order.setDrivePos(featureMap.getOrDefault(DRIVE_POS, ""));
            Double bk = NumberUtils.getDouble(order.getBk());
            if (Objects.nonNull(bk) && bk >= 1060) {
                order.setDrivePos("");
            }

        } catch (Exception e) {
            order.setBz("错误:BK 解析成数字错误");
        }
    }

    private void WL_57860921(Map<String, String> featureMap, LpsOrderXcpVO order) {
        try {
            order.setP00Tsd(featureMap.getOrDefault(P00_TSD, ""));
        } catch (Exception e) {
            order.setBz("错误:解析错误");
        }
    }

    private void WL_57860919(Map<String, String> featureMap, LpsOrderXcpVO order) {
        try {
            order.setTk(featureMap.getOrDefault(TK, ""));
            order.setP00Tsd(featureMap.getOrDefault(P00_TSD, ""));
            order.setP00StructureType(featureMap.getOrDefault(P00_STRUCTURE_TYPE, ""));
        } catch (Exception e) {
            order.setBz("错误:解析错误");
        }
    }

    private void WL_57860119(Map<String, String> featureMap, LpsOrderXcpVO order) {
        try {
            order.setP00Tsd(featureMap.getOrDefault(P00_TSD, ""));
            order.setP00TbType(featureMap.getOrDefault(P00_TB_TYPE, ""));
        } catch (Exception e) {
            order.setBz("错误:解析错误");
        }
    }
}
